package com.springboot.demo.service;

import com.springboot.demo.entity.*;
import com.springboot.demo.mapper.ReaderBRMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class ReaderBRService {
    @Autowired
    ReaderBRMapper readerBRMapper;

    //修改书籍状态为reserved
    public void updateReserved(long bookid){
        readerBRMapper.updateReserved(bookid);
    }

    //修改书籍状态为borrowed
    public void updateBorrowed(long bookid){
        readerBRMapper.updateBorrowed(bookid);
    }

    //修改书籍状态为not borrowed
    public void updateNotBorrowed(long bookid){
        readerBRMapper.updateNotBorrowed(bookid);
    }

    //修改书籍状态为borrowing
    public void updateBorrowing(long bookid){
        readerBRMapper.updateBorrowing(bookid);
    }

    //修改reader表中某人的number，减1
    public void decReaderNum(String rname,String phone){readerBRMapper.decReaderNum(rname, phone);}

    //修改reader表中某人的number，加1
    public void incReaderNum(String rname,String phone){readerBRMapper.incReaderNum(rname, phone);}

    //根据bookid取Book
    public List<Book> getBook(long bookid){
        return readerBRMapper.getBook(bookid);
    }

    //根据rname与phone从reader表中取Reader
    public List<Reader> getReaderByNP(String rname,String phone){return readerBRMapper.getReaderByNP(rname,phone);}

    //从reserve表中取全部Reserve
    public List<Reserve> getReserve(){return readerBRMapper.getReserve();}

    //根据rname与phone与bookid从reserve表中取Reserve
    public List<Reserve> getReserveByRPB(String rname,String phone,long bookid){return readerBRMapper.getReserveByRPB(rname, phone, bookid);}

    //根据phone与rname与state='borrowing'从borrow表中取Borrow
    public List<BorrowMsg> getBorrowing(String phone, String rname){return readerBRMapper.getBorrowing(phone, rname);}

    //根据phone与rname与bookid与state='borrowing'从borrow表中取Borrow
    public List<BorrowMsg> getBorrowingB(String phone,String rname,long bookid){return readerBRMapper.getBorrowingB(phone, rname, bookid);}

    //根据phone与rname与bookid与state='applying'从borrow表中取Borrow
    public List<BorrowMsg> getBorrowingA(String phone,String rname,long bookid){return readerBRMapper.getBorrowingA(phone, rname, bookid);}

    //根据phone与rname与bookid与state='borrowing'或'applying'从borrow表中取Borrow
    public List<BorrowMsg> getBorrowingBA(String phone,String rname,long bookid){return readerBRMapper.getBorrowingBA(phone, rname, bookid);};

    //根据phone与bookid与state='returning'从returntable表中取returning
    public List<ReturnMsg> getReturningPB(String phone, long bookid){return readerBRMapper.getReturningPB(phone, bookid);}

    //向reserve表中添加某人reserve信息的phone与bookid与start_time
    public void insertReserve(String phone,long bookid,String rname,String startTime){readerBRMapper.insertReserve(phone, bookid, rname, startTime);}

    //向borrow表中插入borrow申请信息
    public void insertBorrow(String phone,String rname,long bookid,String startTime){
        readerBRMapper.insertBorrow(phone, rname, bookid, startTime);
    }

    //向return表中插入return申请信息
    public void insertReturn(String phone,long bookid,String startTime,String endTime,String overdue,long days,long fine){
        readerBRMapper.insertReturn(phone, bookid, startTime, endTime, overdue, days, fine);
    }

    //根据phone与bookid删除reserve表中某人reserve信息
    public void deleteReserve(String phone,long bookid){readerBRMapper.deleteReserve(phone,bookid);}

    //字符串转为时间
    public Calendar StringToTime(String time) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dateParse = sdf.parse(time);
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(dateParse);
        return calendar;
    }

    //时间转为字符串
    public String TimeToString(Calendar calendar){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = calendar.getTime();
        String dateStringParse = sdf.format(date);
        return dateStringParse;
    }

    //求时间差，单位为天
    public long TimeMinusTimeDay(Calendar calendar1,Calendar calendar2){
        long oldDate = calendar1.getTime().getTime(); //Date.getTime() 获得毫秒型日期
        long newDate = calendar2.getTime().getTime();

        long betweenDate = (newDate - oldDate) / (1000 * 60 * 60 * 24); //计算间隔多少天，则除以毫秒到天的转换公式
//        System.out.print(betweenDate);

        return betweenDate;
    }

    //求时间差，单位为分钟
    public long TimeMinusTimeMinite(Calendar calendar1,Calendar calendar2){
        long oldDate = calendar1.getTime().getTime(); //Date.getTime() 获得毫秒型日期
        long newDate = calendar2.getTime().getTime();

        long betweenDate = (newDate - oldDate) / (1000 * 60); //计算间隔多少天，则除以毫秒到天的转换公式
//        System.out.print(betweenDate);

        return betweenDate;
    }

}
//    //日期转换及相减使用示例
//    Calendar calendar = Calendar.getInstance();
//    System.out.println(readerBRService.StringToTime(list.get(0).getStartTime()));
//    System.out.println(readerBRService.TimeToString(calendar));
//    System.out.println(readerBRService.TimeMinusTimeDay(readerBRService.StringToTime("2016-01-01 11:11:11"),readerBRService.StringToTime("2016-01-05 21:11:11")));